package cn.iocoder.yudao.module.erp.controller.admin.invoice;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.invoice.vo.InvoicePageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.invoice.vo.InvoiceRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.invoice.vo.InvoiceSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.invoice.InvoiceDO;
import cn.iocoder.yudao.module.erp.service.invoice.InvoiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP 发票")
@RestController
@RequestMapping("/erp/invoice")
@Validated
public class InvoiceController {

    @Resource
    private InvoiceService invoiceService;


    /**
     * 根据发票整合单编号查询发票信息
     * @param checkInvoiceId
     * @return
     */
    @GetMapping("/get-list-by-check-invoice-id")
    public CommonResult<List<InvoiceRespVO>> getInvoiceByCheckInvoiceId(@RequestParam("checkInvoiceId") String checkInvoiceId) {
        List<InvoiceDO> invoiceList = invoiceService.getInvoiceByCheckInvoiceId(checkInvoiceId);
        return success(BeanUtils.toBean(invoiceList, InvoiceRespVO.class));
    }

    // 根据随货同行单的采购单编号数组 查询发票列表
    @GetMapping("/get-list-by-purchase-order-ids")
    public CommonResult<List<InvoiceRespVO>> getInvoiceListByPurchaseOrderIds(@RequestParam("purchaseOrderIds") List<String> purchaseOrderIds) {
        List<InvoiceDO> invoiceList = invoiceService.getInvoiceListByPurchaseOrderIds(purchaseOrderIds);
        return success(BeanUtils.toBean(invoiceList, InvoiceRespVO.class));
    }

    @PostMapping("/create")
    @Operation(summary = "创建ERP 发票")
    @PreAuthorize("@ss.hasPermission('erp:invoice:create')")
    public CommonResult<Long> createInvoice(@Valid @RequestBody InvoiceSaveReqVO createReqVO) {
        return success(invoiceService.createInvoice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 发票")
    @PreAuthorize("@ss.hasPermission('erp:invoice:update')")
    public CommonResult<Boolean> updateInvoice(@Valid @RequestBody InvoiceSaveReqVO updateReqVO) {
        invoiceService.updateInvoice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 发票")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:invoice:delete')")
    public CommonResult<Boolean> deleteInvoice(@RequestParam("id") Long id) {
        invoiceService.deleteInvoice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 发票")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:invoice:query')")
    public CommonResult<InvoiceRespVO> getInvoice(@RequestParam("id") Long id) {
        InvoiceDO invoice = invoiceService.getInvoice(id);
        return success(BeanUtils.toBean(invoice, InvoiceRespVO.class));
    }

    @GetMapping("/getInvoiceByOrderIntegrateId")
    @Operation(summary = "获得ERP 发票")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:invoice:query')")
    public CommonResult<List<InvoiceRespVO>> getInvoiceByOrderIntegrateId(@Valid InvoicePageReqVO pageReqVO) {
        List<InvoiceDO> invoice = invoiceService.getInvoiceByOrderIntegrateId(pageReqVO);
        return success(BeanUtils.toBean(invoice, InvoiceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 发票分页")
    @PreAuthorize("@ss.hasPermission('erp:invoice:query')")
    public CommonResult<PageResult<InvoiceRespVO>> getInvoicePage(@Valid InvoicePageReqVO pageReqVO) {
        PageResult<InvoiceDO> pageResult = invoiceService.getInvoicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InvoiceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 发票 Excel")
    @PreAuthorize("@ss.hasPermission('erp:invoice:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInvoiceExcel(@Valid InvoicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InvoiceDO> list = invoiceService.getInvoicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 发票.xls", "数据", InvoiceRespVO.class,
                        BeanUtils.toBean(list, InvoiceRespVO.class));
    }

}